<?php

/*上传文件控制器+{删除}*/
namespace app\member\controller;
use app\common\Qiniu;
use app\member\controller\db\Image;
use Parsedown;
use think\Controller;
use think\facade\Request;
use think\facade\Session;

class Upload extends Controller
{
/*单文件上传*/
    public function uploadImage(){
        $Parsedown = new  Parsedown();
        if($this->request->isAjax()){
            $upload = new Qiniu();
            $dbFile=new Image();
            $file = request()->file("image");
            $info = $file->move('../public/resource');
            if ($info) {
                $image_md5 = $info->md5();
                if($dbFile->selectByMd5($image_md5)===null){
//                $image_url = $info->getSaveName();
                    $data = [
                        "image_md5"         => $image_md5,
                        "image_url"         => "http://cdn.www.hojay.top/".$image_md5,
                        "image_class"       => $this->request->param("image_class"),
                        "image_content"     => $Parsedown->text($this->request->param("content")),
                        "image_member_name" => Session::get("login"),
                        "image_show"        => $this->request->param("show"),
                    ];
                    $image = new Image();
                    $image->addImage($data);
                }else{
                    /*图片重复删除*/
                    unlink("../public/resource/".$info->getSaveName());
                    return "-1";
                }
            } else {
                // 上传失败获取错误信息
                return $file->getError();
            }
            $msg = $upload->uploadFile($image_md5,"../public/resource/".$info->getSaveName());
            unlink("../public/resource/".$info->getSaveName());
            return $msg;
        }
        return "false";
    }
    /*多文件上传只能切换方式*/

    /*点击硬删除图片信息*/
    public function deleteImageAll(){
        if($this->request->isAjax()){
            $deleted = new Image();
            $qiniu = new Qiniu();
            $datas = $deleted->deletedImage(Session::get("login"));
            if(count($datas)!==0){
                foreach ($datas as $key=>$data){
                    $qiniu->deleteFile($data['image_md5']);
                }
                $deleted->DeleteImage(Session::get("login"));
                return true;
            }else{
                return false;
            }
        }
        return false;
    }
}

